import request from "./request";

export class ArticleApi {
  // 获取所有文章
  static getArticles(params): Promise<[IArticle[], number]> {
    return request.get("/article", { params });
  }
  // 获取分类下所有文章
  static getArticlesByCategory(
    category,
    params
  ): Promise<[IArticle[], number]> {
    return request.get(`/article/category/${category}`, { params });
  }
  // 获取标签下所有文章
  static getArticlesByTag(tag, params): Promise<[IArticle[], number]> {
    return request.get(`/article/tag/${tag}`, { params });
  }

  // 获取推荐文章
  static getRecommend(id): Promise<[IArticle[], number]> {
    return request.get(`/article/recommend`, { params: { articleId: id } });
  }

  // 获得所有文章归档
  static getArchives(): Promise<{
    [key: string]: { [key: string]: IArticle[] };
  }> {
    return request.get("/article/archives");
  }

  // 获取指定文章
  static getArticle(id): Promise<IArticle> {
    return request.get(`/article/${id}`);
  }

  // 新建文章
  static addArticle(data): Promise<IArticle> {
    return request.post("/article/", data);
  }

  // 更新文章
  static updateArticle(id, data): Promise<IArticle> {
    return request.patch(`/article/${id}`, data);
  }
  /**
   * 更新文章阅读量
   * @param id
   * @param data
   */
  static async updateArticleViews(id): Promise<IArticle> {
    return request.post(`/article/${id}/views`);
  }

  /**
   * 校验文章密码是否正确
   * @param id
   * @param password
   */
  static async checkPassword(
    id,
    password
  ): Promise<{ pass: boolean } & IArticle> {
    return request.post(`/article/${id}/checkPassword`, { password });
  }

  /**
   * 删除文章
   * @param id
   */
  static async deleteArticle(id): Promise<IArticle> {
    return request.delete(`/article/${id}`);
  }
}
